home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / ltp_createmenutemplate.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  5KB  |  270 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /*****************************************************************************/
  15.  
  16. #include <dos/dos.h>    /* For AmigaDOS error definitions */
  17.  
  18. /*****************************************************************************/
  19.  
  20. #include "Assert.h"
  21.  
  22. /*****************************************************************************/
  23.  
  24. #ifdef DO_MENUS    /* Support code */
  25.  
  26.     /* LTP_CreateMenuTemplate(RootMenu *Root,LONG *ErrorPtr,struct NewMenu *MenuTemplate):
  27.      *
  28.      *    Create the menu strip data.
  29.      */
  30.  
  31. BOOL
  32. LTP_CreateMenuTemplate(RootMenu *Root,LONG *ErrorPtr,struct NewMenu *MenuTemplate)
  33. {
  34.     MenuNode    *LastMenu    = NULL;
  35.     ItemNode    *LastItem    = NULL;
  36.     ItemNode    *LastSub    = NULL;
  37.  
  38.     MenuNode    *Menu;
  39.     ItemNode    *Item;
  40.     ItemNode    *Sub;
  41.  
  42.     LONG         Error;
  43.  
  44.     BOOL         Done;
  45.  
  46.     Done    = FALSE;
  47.     Error    = 0;
  48.  
  49.     do
  50.     {
  51.             // Check the type
  52.  
  53.         switch(MenuTemplate->nm_Type)
  54.         {
  55.                 // Stop here?
  56.  
  57.             case NM_END:
  58.  
  59.                 DB(kprintf("NM_END\n"));
  60.  
  61.                     // Mark the last menu item
  62.  
  63.                 if(LastSub)
  64.                     LastSub->Flags |= ITEMF_LastItem;
  65.                 else
  66.                 {
  67.                     if(LastItem)
  68.                         LastItem->Flags |= ITEMF_LastItem;
  69.                 }
  70.  
  71.                 Done = TRUE;
  72.                 break;
  73.  
  74.                 // Found a new menu?
  75.  
  76.             case NM_TITLE:
  77.  
  78.                 DB(kprintf("NM_TITLE |%s|\n",MenuTemplate->nm_Label));
  79.  
  80.                     // Stop on weird labels
  81.  
  82.                 if(MenuTemplate->nm_Label == NM_BARLABEL)
  83.                     Error = ERROR_OBJECT_WRONG_TYPE;
  84.                 else
  85.                 {
  86.                         // Choose the menu to use
  87.  
  88.                     if(!LastMenu)
  89.                         Menu = (MenuNode *)&Root->Node;
  90.                     else
  91.                         Menu = NULL;
  92.  
  93.                     DB(kprintf("         make menu\n"));
  94.  
  95.                         // Make room for the menu
  96.  
  97.                     if(Menu = LTP_MakeMenu(Root,Menu,MenuTemplate))
  98.                     {
  99.                             // Link to last menu
  100.  
  101.                         if(LastMenu)
  102.                             LastMenu->Menu.NextMenu = &Menu->Menu;
  103.  
  104.                             // Mark the last menu item
  105.  
  106.                         if(LastSub)
  107.                             LastSub->Flags |= ITEMF_LastItem;
  108.                         else
  109.                         {
  110.                             if(LastItem)
  111.                                 LastItem->Flags |= ITEMF_LastItem;
  112.                         }
  113.  
  114.                         LastMenu    = Menu;
  115.                         LastItem    = NULL;
  116.                         LastSub        = NULL;
  117.  
  118.                             // Add the menu to the list
  119.  
  120.                         AddTail((struct List *)&Root->MenuList,(struct Node *)Menu);
  121.                     }
  122.                     else
  123.                         Error = ERROR_NO_FREE_STORE;
  124.                 }
  125.  
  126.                 break;
  127.  
  128.                 // Found a new menu item?
  129.  
  130.             case NM_ITEM:
  131.  
  132.                 DB(kprintf("NM_ITEM |%s|\n",MenuTemplate->nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : MenuTemplate->nm_Label));
  133.  
  134.                     // We need a menu to attach this to
  135.  
  136.                 if(!LastMenu)
  137.                     Error = ERROR_INVALID_COMPONENT_NAME;
  138.                 else
  139.                 {
  140.                     DB(kprintf("        make item\n"));
  141.  
  142.                         // Make room for the item
  143.  
  144.                     if(Item = LTP_MakeItem(Root,MenuTemplate))
  145.                     {
  146.                             // Link it in
  147.  
  148.                         if(LastItem)
  149.                             LastItem->Item.NextItem = &Item->Item;
  150.                         else
  151.                             LastMenu->Menu.FirstItem = &Item->Item;
  152.  
  153.                         LastItem    = Item;
  154.                         LastSub        = NULL;
  155.  
  156.                             // Add it to the item list
  157.  
  158.                         AddTail((struct List *)&Root->ItemList,(struct Node *)Item);
  159.                     }
  160.                     else
  161.                         Error = ERROR_NO_FREE_STORE;
  162.                 }
  163.  
  164.                 break;
  165.  
  166.             case NM_SUB:
  167.  
  168.                 DB(kprintf("NM_SUB |%s|\n",MenuTemplate->nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : MenuTemplate->nm_Label));
  169.  
  170.                     // We need a menu item to attach this to
  171.  
  172.                 if(!LastItem)
  173.                     Error = ERROR_INVALID_COMPONENT_NAME;
  174.                 else
  175.                 {
  176.                     DB(kprintf("       make sub\n"));
  177.  
  178.                         // Make room for the item
  179.  
  180.                     if(Sub = LTP_MakeItem(Root,MenuTemplate))
  181.                     {
  182.                             // Make sure it stays a submenu item
  183.  
  184.                         Sub->Flags |= ITEMF_IsSub;
  185.  
  186.                             // Link it in
  187.  
  188.                         if(LastSub)
  189.                             LastSub->Item.NextItem = &Sub->Item;
  190.                         else
  191.                         {
  192.                                 // We cannot attach submenu items to
  193.                                 // separator bars
  194.  
  195.                             if(LastItem->Flags & ITEMF_IsBar)
  196.                                 Error = ERROR_INVALID_COMPONENT_NAME;
  197.                             else
  198.                             {
  199.                                 DB(kprintf("-----------> |%s| has submenu items\n",((struct IntuiText *)LastItem->Item.ItemFill)->IText));
  200.  
  201.                                     // Link the submenu item in
  202.  
  203.                                 LastItem->Item.SubItem = &Sub->Item;
  204.  
  205.                                     // Zap the command sequence data
  206.  
  207.                                 LastItem->Flags            = (LastItem->Flags & ~ITEMF_Command) | ITEMF_HasSub;
  208.                                 LastItem->ExtraLabel    = NULL;
  209.  
  210.                                 LastItem->Item.Flags    &= ~COMMSEQ;
  211.                                 LastItem->Item.Command     = 0;
  212.  
  213.                                     // This is the first submenu item for this menu item
  214.  
  215.                                 Sub->Flags |= ITEMF_FirstSub;
  216.                             }
  217.                         }
  218.  
  219.                         LastSub = Sub;
  220.  
  221.                             // Add it to the list
  222.  
  223.                         AddTail((struct List *)&Root->ItemList,(struct Node *)Sub);
  224.                     }
  225.                     else
  226.                         Error = ERROR_NO_FREE_STORE;
  227.                 }
  228.  
  229.                 // Fall through to...
  230.  
  231.                 // Do nothing
  232.  
  233.             case NM_IGNORE:
  234.  
  235.                 break;
  236.         }
  237.  
  238.             // Proceed to the next entry
  239.  
  240.         MenuTemplate++;
  241.     }
  242.     while(!Done && !Error);
  243.  
  244.     DB(kprintf("%s error = %ld\n",__FILE__,Error));
  245.  
  246.         // Did we create anything at all?
  247.  
  248.     if(!Error)
  249.     {
  250.         if(!Root->MenuList.mlh_Head->mln_Succ)
  251.             Error = ERROR_OBJECT_NOT_FOUND;
  252.         else
  253.             LTP_FixExtraLabel(Root,&Error);
  254.     }
  255.  
  256.     DB(kprintf("through, error=%ld\n",Error));
  257.  
  258.     if(Error)
  259.     {
  260.         if(ErrorPtr)
  261.             *ErrorPtr = Error;
  262.  
  263.         return(FALSE);
  264.     }
  265.     else
  266.         return(TRUE);
  267. }
  268.  
  269. #endif    /* DO_MENUS */
  270.